<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/5/3
 * Time: 9:36
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use app\common\model\Floor as FloorModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\Systems as SystemsModel;
use app\common\model\Type as TypeModel;
class Type extends HomeBase{

    protected $type_model;
    protected $floor_model;
    protected $goods_model;
    protected $systems_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->type_model = new TypeModel();
        $this->floor_model = new FloorModel();
        $this->goods_model = new GoodsModel();
        $this->systems_model = new SystemsModel();
    }

    public  function getTypeAndGoods()
    {
        $floor = input('floor');
        //$floor = 1 专版画册    2专版单页    3手提袋
        //成品尺寸
        $size = $this->type_model->getFloorSize($floor);
        //装订方式
        $bindingstyle = $this->goods_model->bindingStyle($floor);
        //材质
        $texturematerial = $this->floor_model->textureMaterial($floor);
        //后道工序
        $postprocess = $this->goods_model->postProcess($floor);
        //查询分类下面最后一种材质的纸张克数
        $papernumber = $this->floor_model->paperNumber($floor);
        $this->assign('size',$size);
        $this->assign('bindingstyle',$bindingstyle);
        $this->assign('texturematerial',$texturematerial);
        $this->assign('postprocess',$postprocess);
        $this->assign('papernumber',$papernumber);
        if ($floor == 1){
            return $this->fetch('onlyalbum');
        }
        if ($floor == 2){
            return $this->fetch('onlypage');
        }
        if ($floor == 3){
            return $this->fetch('bag');
        }

    }
    /**
     * 前台  选择不同的纸张  查询该纸张下面的所有纸张克数
     */
    public function getpagenum()
    {
        $cateId =input('cateid');
        $detail = $this->floor_model->getPageNum($cateId);
        if(!$detail){
            return ['code' => 0];
        }
        return ['code' => 1, 'data' => $detail];
    }

      //订单报价
    public function orderoffer()
    {
        //$floor = 1 专版画册    2专版单页    3手提袋
        $params = input();
        //查询当前用户选择的    开数(num)  (type 1大度 2正度)    长(long) 宽(wide)
        $kaishu = $this->floor_model->getKai(intval($params['chengpin']));
        //查询印刷配置的损耗系统参数
        $system = $this->systems_model->concat();
        $loss = floatval($system['s_loss']);
        $floor = input('floor');
        //画册报价
        if($floor == 1){
            //画册的算法
            //用纸数量  内页
            $pagenum = intval($params['pages'])/2*intval($params['nums'])/intval($kaishu['num'])*$loss;
            //封面纸数量
            $fengpagenum = 2*intval($params['nums'])/intval($kaishu['num'])*$loss;

            //查询纸质的吨价    正度大度参数系数 'price', 'largecoefficient 大度', 'positivecoefficient 正度 '
            $detail = $this->floor_model->pagePriceAndLoss(intval($params['material']));
            $fengdetail = $this->floor_model->pagePriceAndLoss(intval($params['cover']));

            //查询所选择的纸张克数
            $keshu = $this->floor_model->getKe(intval($params['num']));

			//查询所选择的封面纸张克数
            $fengkeshu = $this->floor_model->getKe(intval($params['fengnum']));
            //纸价格  $pageprice
            $pageprice  = 0;
            if($kaishu['type'] == 1){
                //大度  内页纸价
                $price = floatval($detail['price'])/intval($detail['largecoefficient'])*intval($keshu)/500*$pagenum;
                //封面纸价格
                $fengPrice = floatval($fengdetail['price'])/intval($fengdetail['largecoefficient'])*intval($fengkeshu)/500*$fengpagenum;
                //纸总价格
                $pageprice = $price+$fengPrice;
            }else if ($kaishu['type'] == 2){
                //正度   内页纸价
                $price = floatval($detail['price'])/intval($detail['positivecoefficient'])*intval($keshu)/500*$pagenum;
                //封面纸价格
                $fengPrice = floatval($fengdetail['price'])/intval($fengdetail['positivecoefficient'])*intval($fengkeshu)/500*$fengpagenum;
                //纸总价格
                $pageprice = $price+$fengPrice;
            }
            //后道工序价格
            $work = $params['work'];
            $totalPrice = 0;    //后道总价
            //1本总页数  封面数量
            $allPageNums = intval($params['pages']) + 4;
            //总数量
            $num = intval($params['nums']);
            if ($work != ''){
                //用户没有选择后道工序 后道工序的价格为0元
                $workArr = explode(',', $work);
                //选择了后道工序  循环查出选择的价格以及最低价  然后求和
//                $len = sizeof($workArr);

                //总平方米  尺寸*总数量
                $allSquare = $kaishu['long']/1000*$kaishu['wide']/1000*intval($params['nums']);

                foreach($workArr as $k=>$v){
//   'calculationtype 计算类型（1 :页数   2:平方米  3： 本）', 'unitprice 单价', 'minprice 最低价'
                    $goodsDetail = $this->goods_model->getGoodsdetail(intval($v));
                    if($goodsDetail['calculationtype'] == 1){
                        $totalPrice += $allPageNums *30;
                    }
                    if($goodsDetail['calculationtype'] == 2){
                        if ($v == 6){   //局部UV
                            if($params['j_chang'] !='' && $params['j_kuan'] !=''){
                                $mianji = intval($params['j_chang'])/1000 * intval($params['j_kuan'])/1000;
                                $jubu_price = $mianji * $num * floatval($goodsDetail['unitprice']);
                                if($jubu_price < floatval($goodsDetail['minprice'])){
                                    $jubu_price = floatval($goodsDetail['minprice']);
                                }
                                $totalPrice += $jubu_price;
                            }else{
                                return ['code' => 0, 'msg' => '局部UV长宽不能为空'];
                            }

                        }else if($v == 8){  //烫金
                            if($params['j_chang'] !='' && $params['j_kuan'] !=''){
                                $mianji = intval($params['t_chang'])/1000 * intval($params['t_kuan'])/1000;
                                $tangjin_price = $mianji * $num * floatval($goodsDetail['unitprice']);
                                if($tangjin_price < floatval($goodsDetail['minprice'])){
                                    $tangjin_price = floatval($goodsDetail['minprice']);
                                }
                                $totalPrice += $tangjin_price;
                            }else{
                                return ['code' => 0, 'msg' => '烫金长宽不能为空'];
                            }

                        }else{
                            $pingmiPrice = 4*$num*$kaishu['long']/1000*$kaishu['wide']/1000*floatval($goodsDetail['unitprice']);
                            $minprice = floatval($goodsDetail['minprice']);
                            if ($pingmiPrice < $minprice){
                                $pingmiPrice = $minprice;
                            }
                            $totalPrice += $pingmiPrice;
                        }
                    }
                    if($goodsDetail['calculationtype'] == 3){
                        $numPrice = $num*floatval($goodsDetail['unitprice']);
                        if($v != 9){
                            if($numPrice < floatval($goodsDetail['minprice'])){
                                $numPrice = floatval($goodsDetail['minprice']);
                            }
                        }
                        $totalPrice += $numPrice;
                    }
                }
            }

            //装订方式价格
            $zhuangdingDetail = $this->goods_model->getGoodsdetail(intval($params['binding']));
            $zhuangding_price = $allPageNums * $zhuangdingDetail['unitprice'] * $num;
            if($zhuangding_price < floatval($zhuangdingDetail['minprice'])){
                $zhuangding_price = floatval($zhuangdingDetail['minprice']);
            }
            //印工费用
            //印工	对开200元（含1000印）	超过每千印加40元
            $page_num = intval($kaishu['num']) / 2 ;   //每开多少页
            $kaishu_num = ceil($allPageNums / $page_num);
            $duikai_num = ceil($num /1000);
            $yingong_price = (200 +  ($duikai_num-1) * 40) * $kaishu_num;
            $allmoney = round($pageprice + $totalPrice + $zhuangding_price + $yingong_price);
            return ['code' => 1, 'data' => $allmoney, 'time' => date('Y-m-d H:i:s'), 'system' => $system, 'nickname' => $this->user['nickname'],'headimgurl' => $this->user['headimgurl']];

        }
        //单页报价
        if($floor == 2){
            /**单页算法 纸张+印费
             * 纸张算法   大度吨价 / 1884 * 克数 / 500 * 数量 / 开数 * 1.08
             *            正度吨价 / 2327 * 克数 / 500 * 数量 / 开数 * 1.08
             *
             * 印费算法   对开 200/套 （含1000印）  1000印以上每加30元
             */
            //用纸数量
            $pagenum = intval($params['nums'])/intval($kaishu['num'])*$loss;
            //数量 5000   开数  16   损耗  1.2
            //查询纸质的吨价    正度大度参数系数 'price', 'largecoefficient 大度', 'positivecoefficient 正度 '
            $detail = $this->floor_model->pagePriceAndLoss(intval($params['material']));
            //  price  7200   大度1884    正度2327
            //查询所选择的纸张克数
            $keshu = $this->floor_model->getKe(intval($params['num']));//200g
            //纸价格  $pageprice
            $pageprice  = 0;
            if($kaishu['type'] == 1){
                //大度  纸价
                $pageprice = floatval($detail['price'])/intval($detail['largecoefficient'])*intval($keshu)/500*$pagenum;

            }else if ($kaishu['type'] == 2){
                //正度 纸价
                $pageprice = floatval($detail['price'])/intval($detail['positivecoefficient'])*intval($keshu)/500*$pagenum;

            }
            $page_num = intval($kaishu['num']) / 2 ;   //每开多少页
            $kaishu_num = intval($params['nums']) / $page_num;//印数
            if(intval($params['danshuang']) == 1) {
                //单印
                $duikai_num = ceil($kaishu_num /1000);
            }
            if(intval($params['danshuang']) == 2) {
                //双印
                $duikai_num = ceil($kaishu_num*2 /1000);
            }

            //$duikai_num = ceil(intval($params['nums']) /1000);
            $yingong_price = 200 +  ($duikai_num-1) * 30;

            $allmoney = round($pageprice  + $yingong_price);
            return ['code' => 1, 'data' => $allmoney, 'time' => date('Y-m-d H:i:s'), 'system' => $system, 'nickname' => $this->user['nickname'],'headimgurl' => $this->user['headimgurl']];

        }
        //手提袋报价
        if($floor == 3){
            /**手提袋报价
             *  材质算法    大度吨价 / 系数 * 克数 / 500 / 开数 * 数量 * 1.2
             *              正度吨价 / 系数 * 克数 / 500 / 开数 * 数量 * 1.2
             *
             * 印工       300元（含1000印）  超过1000加50
             */
            //手提袋的算法
            //查询纸质的吨价    正度大度参数系数 'price', 'largecoefficient 大度', 'positivecoefficient 正度 '
            $detail = $this->floor_model->pagePriceAndLoss(intval($params['material']));
            //查询所选择的纸张克数
            $keshu = $this->floor_model->getKe(intval($params['num']));
            //纸价格  $pageprice
            $pageprice  = 0;
            if($kaishu['type'] == 1){
                //大度  内页纸价
                $pageprice = floatval($detail['price'])/intval($detail['largecoefficient'])*intval($keshu)/500/intval($kaishu['num'])*intval($params['nums'])*$loss;
            }else if ($kaishu['type'] == 2){
                //正度   内页纸价
                $pageprice = floatval($detail['price'])/intval($detail['positivecoefficient'])*intval($keshu)/500/intval($kaishu['num'])*intval($params['nums'])*$loss;

            }
            //后道工序价格
            $work = $params['work'];
            $totalPrice = 0;    //后道总价
            //1本总页数  封面数量
//            $allPageNums = intval($params['pages']) + 4;
            //总数量
            $num = intval($params['nums']);
            if ($work != ''){
                //用户没有选择后道工序 后道工序的价格为0元
                $workArr = explode(',', $work);
                //选择了后道工序  循环查出选择的价格以及最低价  然后求和
//                $len = sizeof($workArr);

                //总平方米  尺寸*总数量
                $allSquare = $kaishu['long']/1000*$kaishu['wide']/1000*intval($params['nums']);

                foreach($workArr as $k=>$v){
//   'calculationtype 计算类型（1 :页数   2:平方米  3： 本）', 'unitprice 单价', 'minprice 最低价'
                    $goodsDetail = $this->goods_model->getGoodsdetail(intval($v));

                    if($goodsDetail['calculationtype'] == 1){
                        //张数计算价格
                        $zhangshu = $num*floatval($goodsDetail['unitprice']);
                        if ($zhangshu < floatval($goodsDetail['minprice'])){
                            $zhangshu = floatval($goodsDetail['minprice']);
                        }
                        $totalPrice += $zhangshu;
                    }
                    if($goodsDetail['calculationtype'] == 2){
                        if ($v == 17){   //局部UV
                            if($params['j_chang'] !='' && $params['j_kuan'] !=''){
                                $mianji = intval($params['j_chang'])/1000 * intval($params['j_kuan'])/1000;
                                $jubu_price = $mianji * $num * floatval($goodsDetail['unitprice']);
                                if($jubu_price < floatval($goodsDetail['minprice'])){
                                    $jubu_price = floatval($goodsDetail['minprice']);
                                }
                                $totalPrice += $jubu_price;
                            }else{
                                return ['code' => 0,'msg' => '局部UV长宽不能为空'];
                            }

                        }else if($v == 15){  //烫金
                            if($params['t_chang'] !='' && $params['t_kuan'] !=''){
                                $mianji = intval($params['t_chang'])/1000 * intval($params['t_kuan'])/1000;
                                $tangjin_price = $mianji * $num * floatval($goodsDetail['unitprice']);
                                if($tangjin_price < floatval($goodsDetail['minprice'])){
                                    $tangjin_price = floatval($goodsDetail['minprice']);
                                }
                                $totalPrice += $tangjin_price;
                            }else{
                                return ['code' => 0,'msg' => '烫金长宽不能为空'];
                            }

                        }else{
                            $pingmiPrice = 4*$num*$kaishu['long']/1000*$kaishu['wide']/1000*floatval($goodsDetail['unitprice']);
                            $minprice = floatval($goodsDetail['minprice']);
                            if ($pingmiPrice < $minprice){
                                $pingmiPrice = $minprice;
                            }
                            $totalPrice += $pingmiPrice;
                        }
                    }
                    if($goodsDetail['calculationtype'] == 3){
                        $numPrice = $num*floatval($goodsDetail['unitprice']);
                        if($v != 20  && $v != 21){
                            if($numPrice < floatval($goodsDetail['minprice'])){
                                $numPrice = floatval($goodsDetail['minprice']);
                            }
                        }
                        $totalPrice += $numPrice;
                    }
                }
            }
            //印工	对开300元（含1000印）	超过每千印加50元
            //$page_num = intval($kaishu['num']) / 2 ;   //每开多少页
            //$kaishu_num = ceil($num / $page_num);
            //$duikai_num = ceil($num /1000);
			$duikai_num = ceil($params['nums'] /1000);
            $yingong_price = (300 +  ($duikai_num-1) * 50);
            $allmoney = round($pageprice + $totalPrice  + $yingong_price);
            return ['code' => 1, 'data' => $allmoney, 'time' => date('Y-m-d H:i:s'), 'system' => $system, 'nickname' => $this->user['nickname'],'headimgurl' => $this->user['headimgurl']];

        }


    }

















}